package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.BusyException;
import com.xinzhitong.www.exception.checkcode.CheckCodeInvalidException;
import com.xinzhitong.www.exception.checkcode.WrongCheckCodeException;
import com.xinzhitong.www.exception.email.InvalidEmailException;
import com.xinzhitong.www.exception.phone.ContainsOtherCharacterException;
import com.xinzhitong.www.exception.phone.PhoneLengthNotStandardException;
import com.xinzhitong.www.exception.test.OutOfTimeException;
import com.xinzhitong.www.utils.Message;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局捕捉异常的回应
 *
 * @author 徐鑫
 */
@ControllerAdvice
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public Message unauthorized() {
        return Message.createErr("对不起，您没有权限访问");
    }

    @ExceptionHandler(BusyException.class)
    @ResponseBody
    public Message busy(BusyException e) {
        return Message.createErr("系统繁忙，请稍后再试");
    }

    @ExceptionHandler(CheckCodeInvalidException.class)
    @ResponseBody
    public Message checkCodeInvalid(CheckCodeInvalidException e) {
        return Message.createErr("请先获取验证码");
    }

    @ExceptionHandler(WrongCheckCodeException.class)
    @ResponseBody
    public Message wrongCheckCode(WrongCheckCodeException e) {
        return Message.createErr("验证码错误");
    }

    @ExceptionHandler(PhoneLengthNotStandardException.class)
    @ResponseBody
    public Message phoneLengthNotStandardException(PhoneLengthNotStandardException e) {
        return Message.createErr("请输入正确的手机号");
    }

    @ExceptionHandler(ContainsOtherCharacterException.class)
    @ResponseBody
    public Message containsOtherCharacterException(ContainsOtherCharacterException e) {
        return Message.createErr("请输入正确的手机号");
    }

    @ExceptionHandler(OutOfTimeException.class)
    @ResponseBody
    public Message outOfTime(OutOfTimeException e) {
        e.printStackTrace();
        return Message.createErr("测试已结束");
    }

    @ExceptionHandler(InvalidEmailException.class)
    @ResponseBody
    public Message invalidEmail(InvalidEmailException e) {
        e.printStackTrace();
        return Message.createErr("请输入有效的邮箱地址");
    }

}
